﻿using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SerialPortFunction
{
    public class SerialHelper : SerialBase
    {
        private static readonly object Lock = new object();
        //private static SerialHelper _instance;
        //public static SerialHelper Instaince
        //{
        //    get
        //    {
        //        if (_instance == null)
        //        {
        //            lock (Lock)
        //            {
        //                if (_instance == null)
        //                {
        //                    _instance = new SerialHelper();
        //                }
        //            }
        //        }
        //        return _instance;
        //    }
        //}
        public SerialBase Serial;
        public delegate void SerialPortDataReceiveDelegates(object sender, SerialDataReceivedEventArgs e, byte[] bits);
        public event SerialPortDataReceiveDelegates DataReceiveds;
        public SerialHelper()
        {

        }

        public Parity InitParity(string name)
        {
            Parity parity;
            switch (name)
            {
                case "NONE":
                    parity = Parity.None;
                    break;
                case "ODD":
                    parity = Parity.Odd;
                    break;
                case "EVEN":
                    parity = Parity.Even;
                    break;
                case "MARK":
                    parity = Parity.Mark;
                    break;
                case "SPACE":
                    parity = Parity.Space;
                    break;
                default:
                    parity = Parity.None;
                    break;
            }
            return parity;
        }

        public StopBits InitStopBits(string name)
        {
            StopBits stopBits;
            switch (name)
            {
                case "1":
                    stopBits = StopBits.One; //使用一个停止位
                    break;
                case "1.5f":
                    stopBits = StopBits.OnePointFive; //使用一个停止位
                    break;
                case "2":
                    stopBits = StopBits.Two; //使用一个停止位
                    break;
                default:
                    stopBits = StopBits.One;
                    break;
            }
            return stopBits;
        }
        public string[] GetPortName()
        {
            return System.IO.Ports.SerialPort.GetPortNames();
        }
        public bool Open(string PortName, int baudRate, Parity parity, int dataBits, StopBits stopBits)
        {
            bool _isConnect = false;
            try
            {
                if (Serial == null)
                {
                    Serial = new SerialBase(PortName, baudRate, parity, dataBits, stopBits);
                    _isConnect = Serial.openPort();
                    Serial.DataReceived += Serial_DataReceived;
                }
                return _isConnect;
            }
            catch (Exception)
            {
                return _isConnect;
            }
        }
        public void SendData(byte[] data, int offset, int count)
        {
            Serial.SendData(data, offset, count);
        }
        public bool SendData(string  data)
        {
            try
            {
                Serial.SendData(data);
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }
        private void Serial_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e, byte[] bits)
        {
            DataReceiveds?.Invoke(sender, e, bits);
        }
        public void Close()
        {
            Serial.closePort();
        }
    }
}
